#include "head.h"

int whichOrder(char* orderList,int* orderflag,char* args)
{
    char* order = strtok(orderList," ");
    char* tmp = strtok(NULL," ");
    if(tmp != NULL)
    {
        memcpy(args,tmp,strlen(tmp));
    }
    char buf[4];
    strncpy(buf,orderList,3);
    buf[3] = '\0';

    if(strcmp(order,"cd") == 0)
    {
        *orderflag = CD;
        //printf("cd\n");
    }
    else if(strcmp(order,"ls") == 0)
    {
        *orderflag = LS;
        //printf("ls\n");
    }
    else if(strcmp(order,"puts") == 0)
    {
        *orderflag = PUTS;
        //printf("puts\n");
    }
    else if(strcmp(order,"gets") == 0)
    {
        *orderflag = GETS;
        //printf("gets\n");
    }
    else if(strcmp(order,"remove") == 0)
    {
        *orderflag = REMOVE;
        //printf("remove\n");
    }
    else if(strcmp(buf,"pwd") == 0)
    {
        *orderflag = PWD;
        //printf("pwd\n");
    }
    else if(strcmp(order,"mkdir") == 0)
    {
        *orderflag = MKDIR;
        //printf("mkdir\n");
    }
    else if(strcmp(order,"rmdir") == 0)
    {
        *orderflag = RMDIR;
        //printf("rmdir\n");
    }
    else if(strcmp(order,"gets2") == 0)
    {
        *orderflag = GETS2;
    }
    else
    {
        printf("Illegal Order!\n");
        return -1;
    }

    return 0;
}

int transOrder(int netfd,int orderflag,char* args)
{
    train_t train;
    bzero(&train,sizeof(train));
    train.length = sizeof(int) + strlen(args);
    sprintf(train.data,"%d%s",orderflag,args);
    send(netfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    return 0;
}
int pwdRecv(int sockfd)
{
    int length;
    char buff[1024]={0};
    recv(sockfd,&length,sizeof(int),MSG_WAITALL);
    recv(sockfd,buff,length,MSG_WAITALL);
    printf("%s\n",buff);
    return 0;
}

 int lsRecv(int netfd)
     {
   
         char buff[1024]={0};
         int length;
         while(1)
         {
             memset(buff,0,sizeof(buff));
             int ret=recv(netfd,&length,sizeof(int),MSG_WAITALL);
             if(length==0)
             {
                 break;
             }
             recv(netfd,buff,length,MSG_WAITALL);
            printf("%s\n",buff);
         }
    return 0;
     }

int Login(char* username,char* userpasswd,int sockfd)
{
    train_t train;
    train.length = strlen(username);
    strcpy(train.data,username);
    send(sockfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    bzero(&train,sizeof(train));
    train.length = strlen(userpasswd);
    strcpy(train.data,userpasswd);
    send(sockfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    char loginFlag;
    recv(sockfd,&loginFlag,sizeof(char),MSG_WAITALL);
    if(loginFlag == '1')
    {
        return 0;
    }
    else
    {
        return -1;
    }
}
   
int gets_client(int netfd){
    printf("gets_client has started\n");
    char filename[1024]={0};
    int length;

    recv(netfd,&length,sizeof(int),MSG_WAITALL);
    recv(netfd,filename,length,0);

    printf("have receive the name!\n");

    int fd = open(filename,O_RDWR|O_CREAT|O_TRUNC,0666);
    char buf[1024]={0};

    while(1){
        recv(netfd,&length,sizeof(int),MSG_WAITALL);

        if(length == 0){
            break;
        }
        bzero(buf,sizeof(buf));
        recv(netfd,buf,length,MSG_WAITALL);
        write(fd,buf,length);
    }

    printf("Client have received the whole file!\n");
    close(fd);
    return 0;
}

int puts_client(int netfd,char*path,char*filename){
      train_t train;
      train.length=strlen(path);
      strcpy(train.data,path);
      send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);//文件名
 
       int fd = open(filename,O_RDWR);
      while(1){
           bzero(&train,sizeof(train));
          ssize_t ret = read(fd,train.data,sizeof(train.data));
           train.length = ret;
          send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);//文件内>
          if(ret == 0){
               break;
          }
      }
      close(fd);
       return 0;
 }

int gets_client2(int netfd,char *filename){
    printf("gets_client2 has started!\n");
    char tmpbuf[16]={0};
    recv(netfd,tmpbuf,sizeof(char),MSG_WAITALL);

    printf("shake hand\n");
    printf("filename is %s",filename);
    int ret = access(filename,F_OK);
    printf("access = %d\n",ret);

    int curSize;

    if(ret == -1){
        //文件不存在
        curSize = 0;
        printf("文件不存在\n");
    }
    else{
        //文件存在
        struct stat curStat;
        stat(filename,&curStat);
        curSize = curStat.st_size;

        printf("文件存在，大小为%d\n",curSize);
    }

    send(netfd,&curSize,sizeof(int),MSG_NOSIGNAL);
    printf("gets_client2 has sent the curSize.!\n");

    int fd = open(filename,O_RDWR|O_CREAT,0666);
    printf("cilent has open the file");

    char buf[1024]={0};
    int length;
    lseek(fd,curSize,SEEK_SET);
    printf("client has lseek");


    while(1){
        recv(netfd,&length,sizeof(int),MSG_WAITALL);

        if(length == 0){
            break;
        }
        bzero(buf,sizeof(buf));
        recv(netfd,buf,length,MSG_WAITALL);
        write(fd,buf,length);
    }

    printf("Client has received the whole file!\n");
    close(fd);
}
